<?php

//后台的配置文件
include(dirname(__FILE__).'/config.php');
include(dirname(__FILE__).'/../inc/tree.class.php');



$dopost = isset($_REQUEST['dopost']) ? trim($_REQUEST['dopost']) : 'list';


/***************************************************************************/
if( 'add' == $dopost ){

	$pid = isset($_GET['pid']) ? intval( $_GET['pid'] ) : 0 ;

	//默认值
	$art = array();
	$art['tid'] =  '';
	$art['typename'] =  '';
	$art['display'] =  1 ;
	$art['sort'] =  50;
	$art['litpic'] =  '';
	$art['type'] =  '1';
	$art['jumpurl'] =  '';

	$art['templist'] =  'templist.php';
	$art['tempindex'] =  'tempindex.php';
	$art['tempart'] =  'tempart.php';

	$art['keywords'] =  '';
	$art['description'] = '';
	$art['body'] = '';
	
	$action = '添加';


	$typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
	$tree = new tree();//类实例化为对象
	$tree->init( $typelist );//初始化数据  
	$typelist =    $tree->get_tree_list( 0 , $pid );





	include('temp/type_upt.php');
/***************************************************************************/
}else if( 'upt' == $dopost ){
	
	$tid = isset($_REQUEST['tid']) ? intval($_REQUEST['tid']) : 0;
	//读取当前文章
	$sql = "select * from `type` where tid='{$tid}'";
	$art = dbRow($sql);
	//var_dump($art);
	

	$typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
	$tree = new tree();//类实例化为对象
	$tree->init( $typelist );//初始化数据  
	$typelist =    $tree->get_tree_list( 0 , $art['pid'] );
	
	
	$action = '修改';
	include('temp/type_upt.php');
/***************************************************************************/
}else if( 'save' == $dopost ){
	// addsave  uptsave 

	$tid = isset($_POST['tid']) ? intval($_POST['tid']) : 0;

	//var_dump($_POST);	

	$art = array();
	$art['pid'] =      intval($_POST['pid']) ;

	$art['typename'] =  trim($_POST['typename']) ;
	$art['display'] =   isset($_POST['display']) ? intval($_POST['display']) : 0  ;//多选框 不选择没有值  
	$art['sort'] =      intval($_POST['sort']) ;
	

	$art['litpic'] =   trim($_POST['litpic']) ;
	$art['type'] =     intval($_POST['type']);
	$art['jumpurl'] =  trim($_POST['jumpurl']) ;

	$art['templist'] =   trim($_POST['templist']) ;
	$art['tempindex'] =  trim($_POST['tempindex']) ;
	$art['tempart'] =    trim($_POST['tempart']) ;

	$art['keywords'] =    trim($_POST['keywords']) ;
	$art['description'] = trim($_POST['description']) ;
	$art['body'] =        trim($_POST['body']) ;
	



	//对数据合法性的验证
	if( $art['typename'] == ''  ){

		die('{"ret":1,"msg":"栏目名称 必填项 ！！！"}');

	}


	sec($art);// 递归的 sql 转义字符过滤 
	if($tid){
		//修改
		$action = '修改';
		//
		$typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
		$tree = new tree();//类实例化为对象
		$tree->init( $typelist );//初始化数据  
		$ids =    $tree->getIds( $tid );
		$ids = explode(',', $ids);
		if( in_array( $art['pid']  , $ids )   ){

			die('{"ret":1,"msg":"上级栏目非法 ！！！"}');
		}


		$rs = dbUpdate('type',$art," tid = '{$tid}'"); // 表 数据 条件

	}else{
		//添加
		$action = '添加';
		$rs = dbInsert('type',$art); // 表 数据 
	}


	if($rs){
		//msgBox($action.'成功!!!' ,'?t='.time() , 2 );
		$j = array();
		$j['ret'] = 0;
		$j['msg'] = $action.'成功!!!';
		$j['url'] = '?t='.time() ;

	}else{
		//msgBox($action.'失败!!!' ,'?t='.time() , 10 );
		$j = array();
		$j['ret'] = 999;
		$j['msg'] = '数据保存错误！！！';
		

	}

	die(json_encode($j));




/***************************************************************************/
}else if( 'del' == $dopost ){
	$tid = isset($_GET['tid']) ? intval($_GET['tid']) : 0;

	
	$typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
	$tree = new tree();//类实例化为对象
	$tree->init( $typelist );//初始化数据  
	
	//如果当前栏目下 有子栏目 不可以删除栏目 
	$child_arr =    $tree->get_child_all($tid);
	if($child_arr){ die('{"ret":1 , "msg":"该栏目下有【子栏目】不可以删除！！！"}');};
	
	//如果当前栏目下 有文章 不可以删除栏目 
	//在文章表中能找到一条就行
	$ids = $tree->getIds($tid);
	$sql = "select id from article where tid in ({$ids}) limit 0,1";
	if( dbList($sql) ){
		die('{"ret":2, "msg":"当前栏目下有【文章】 不可以删除栏目 !!!"}');
	}
	
	$action = '删除';
	
	$rs = dbDelete('type'," tid = '{$tid}'"); // 表 条件
	if($rs){
		$j = array();
		$j['ret'] = 0;
		$j['msg'] = $action.'成功!!!';
		$j['url'] = '?t='.time() ;
		die(json_encode($j));
	}else{
		$j = array();
		$j['ret'] = 999;
		$j['msg'] = '数据保存错误！！！';
		die(json_encode($j));
	}
/***************************************************************************/
}else if( 'batch_del' == $dopost ){
	$tid_arr = isset($_POST['tid']) ? $_POST['tid'] : array() ;
	if(!$tid_arr){die('{"ret":9999,"msg":"请先选择！"}');}
	//print_r($tid_arr);
	
	die('{"ret":999,"msg":"不支持批量删除栏目!!!<br/>栏目删除有风险!!!<br/>小心谨慎!!!<br/>"}');
	
/***************************************************************************/
}else if( 'batch_move' == $dopost ){
	$pid = isset($_POST['movetid']) ? intval($_POST['movetid']) :0;
	$tid_arr = isset($_POST['tid']) ? $_POST['tid'] : array() ;
	if(!$tid_arr){die('{"ret":9999,"msg":"请先选择！"}');}
	//print_r($tid_arr);
	
	$typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
	$tree = new tree();//类实例化为对象
	$tree->init( $typelist );//初始化数据  
	
	$err_arr = array();
	foreach($tid_arr as $k => $v ){
		
		$ids =    $tree->getIds( $k );
		$ids = explode(',', $ids);
		if( in_array( $pid  , $ids )   ){
			$err_arr[]='移动['.$k.']到['.$pid.']，失败！！';
			continue;//跳出本次循环
		}else{
			//$err_arr[]='移动['.$k.']到['.$pid.']，成功！！';
			
		
		}
		$art=array();
		$art['pid'] = $pid;
		$rs = dbUpdate('type',$art," tid = '{$k}'"); // 表 数据 条件
	
	}
	$err_msg = implode( '<br/>' , $err_arr );
	if($err_msg){
		die('{"ret":666,"msg":"'.$err_msg.'"}');
	}else{
		die('{"ret":0,"msg":"全部移动成功！！！"}');
	}
	
	
	/***************************************************************************/
}else if( 'batch_sort' == $dopost ){
	
	$url = $_POST['url'];
	$sort_arr = $_POST['sort'];
	foreach($sort_arr as $k => $v){
		$art = array();
		$art['sort'] = $v;
		$rs = dbUpdate('type',$art," tid = '{$k}'"); // 表 数据 条件
	}
	
	//msgBox('更新排序成功，刷新当前页面!!!' ,$url , 3 );
	$j = array();
	$j['ret'] = 0;
	$j['msg'] = '操作成功!!!';
	$j['url'] = $url ;
	die(json_encode($j));


	
	
	
/***************************************************************************/
}else if( 'batch_display' == $dopost ){
	




	//var_dump($_GET);
	//var_dump($_POST);
	$url = $_POST['url'];
	/*
	'sort' => 
    array (size=4)
      5 => string '50' (length=2)
      4 => string '50' (length=2)
      1 => string '50' (length=2)
      3 => string '51' (length=2)
	*/
	$sort_arr = $_POST['sort'];
	$display_arr = $_POST['display'];

	foreach($sort_arr as $k => $v){

		$art = array();
		if( isset( $display_arr[$k] ) && $display_arr[$k] ){
			$art['display'] = 1; 

		}else{
			$art['display'] = 0; 

		}


		$rs = dbUpdate('type',$art," tid = '{$k}'"); // 表 数据 条件
	}
	
	//msgBox('更新排序成功，刷新当前页面!!!' ,$url , 3 );
		$j = array();
		$j['ret'] = 0 ;
		$j['msg'] = 'ok!';


		die(json_encode($j) );

/***************************************************************************/
}else if( 'show' == $dopost ){
	$tid = isset($_GET['tid'])?intval($_GET['tid']):0;	
	
	$url = '/type.php?tid='.$tid;
	
	header('location:'.$url);
	die();
/***************************************************************************/
}else if( 'list' == $dopost ){

	//读取分类列表
	$typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
	//var_dump($typelist);
	//
	$tree = new tree();//类实例化为对象
	$tree->init( $typelist );//初始化数据  
	$typelist =    $tree->get_tree_list( 0 , 0);


	
	include('temp/type_list.php');
/***************************************************************************/
}else{
	die('404');
}
/***************************************************************************/

function typelistOption( $select_id = 0 ){

	$typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
	$tree = new tree();//类实例化为对象
	$tree->init( $typelist );//初始化数据  
	$typelist =    $tree->get_tree_list( 0 , $select_id ,'' );
	$html = '';
	foreach($typelist as $k => $v ){
		$html .=" <option value=\"{$v['tid']}\"  {$v['_selected']} > {$v['_prefix']}{$v['typename']}</option> ";
	}
	return $html;
}